<template>
  <div class="list" ref="wraper">
    <div>
      <div class="area">
        <div class="title border-topbottom">当前城市</div>
        <div class="btn-list">
          <div class="btn-wraper">
            <div class="btn">{{this.currentCity}}</div>
          </div>
        </div>
      </div>
      <div class="area">
        <div class="title border-topbottom">热门城市</div>
        <div class="btn-list">
          <div class="btn-wraper" v-for="item in cityList" :key="item.id" @click="handleCity(item.name)">
            <div class="btn">{{item.name}}</div>
          </div>
        </div>
      </div>
      <div class="area" v-for="(item, key) in allCity" :key="item.key" :ref="key">
        <div class="title border-topbottom">{{key}}</div>
        <div class="item-list">
          <div class="item border-bottom" v-for="city in item" :key="city.id" @click="handleCity(city.name)">{{city.name}}</div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import BScroll from 'better-scroll'
import { mapMutations, mapState } from 'vuex'
export default {
  name: 'CityList',
  props: {
    cityList: Array,
    allCity: Object,
    alphabet: String
  },
  mounted () {
    this.scroll = new BScroll(this.$refs.wraper)
  },
  computed: {
    ...mapState({
      currentCity: 'city'
    })
  },
  watch: {
    alphabet () {
      //  console.log(this.alphabet)
      if (this.alphabet) {
        var element = this.$refs[this.alphabet][0]
        this.scroll.scrollToElement(element)
      }
    }
  },
  methods: {
    handleCity (cityName) {
      // this.$store.commit('changeCity', cityName)
      this.changeCity(cityName)
      this.$router.push('/')
    },
    ...mapMutations(['changeCity'])
  }
}
</script>

<style lang="stylus" scoped>
  @import '~styles/varibles.styl'
  .border-topbottom
    &:before
      border-color: #ccc
    &:after
      border-color: #ccc
  .border-bottom
    &:before
      border-color: #ccc
 .list
   overflow: hidden
   position: absolute
   top: 1.58rem
   left: 0
   right: 0
   bottom: 0
  .title
    height: 0.44rem
    line-height: 0.44rem
    background: #eee
    padding-left: 0.2rem
    color: #666
    font-size: 0.26rem
  .btn-list
    overflow: hidden
    padding: 0.1rem 0.6rem 0.1rem 0.2rem
    .btn-wraper
      width: 33.3%
      float: left
      .btn
        border: 0.02rem solid #ccc
        margin: 0.1rem
        text-align: center
        border-radius: 0.1rem
  .item-list
    .item
      height: 0.76rem
      line-height: 0.76rem
      padding-left: 0.2rem
</style>
